package com.ds.ui {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * @author doctorstal
	 */
	public class SmartContainer extends Sprite{
		public var padding:Number=5;
		public var vertical:Boolean=false;
		public function SmartContainer(){
			super();
			addEventListener(Event.ADDED, addedHandler);
		}

		private function addedHandler(event : Event) : void {
			correctPositions();
		}
		
		private function correctPositions():void{
			var sx:Number=0;
			for(var i:int=0;i<numChildren;i++){
				var child:DisplayObject=getChildAt(i);
				setPos(child,sx);
				sx+=getSize(child)+padding;
			}
		}
		
		private function setPos(child:DisplayObject,pos:Number):void{
			if(vertical){
				child.y=pos;
			}else{
				child.x=pos;
			}
		}
		private function getSize(child:DisplayObject):Number{
			return vertical?child.height:child.width;
		}
	}
}
